package com.jgp.sys.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.common.pojo.LabelValue;
import com.jgp.common.pojo.TreeBean;
import com.jgp.mico.client.MicroClient;
import com.jgp.mico.client.UriVariableStyle;
import com.jgp.sys.model.Attribute;
import com.jgp.sys.props.AttrPropBean;
import com.jgp.sys.service.AttributeService;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.sys.utils.SpringContextUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-01
 */
@Log4j2
@RestController
@RequestMapping("/sys/dd/param-api-ctl")
public class DdParamApiController extends JGPBackController {
    
    @Autowired
    private AttrPropBean propBean;
    
    @Autowired
    private AttributeService service;
    
    @RequestMapping("save-group")
    public Result saveGroup(Attribute group) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/save-group", Result.class, group);
        }
        service.saveGroup(group);
        return ajaxRe(true);
    }
    
    @RequestMapping("save-list")
    public Result saveList(Attribute attr) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/save-list", Result.class,attr);
        }
        service.saveList(attr);
        return ajaxRe(true);
    }
    
    
    @RequestMapping("save-list-item")
    public Result saveListItem(Attribute attr) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/save-list-item", Result.class,attr);
        }
        service.saveListItem(attr);
        return ajaxRe(true);
    }
    
    @RequestMapping("query-group-tree")
    public List<TreeBean> queryGroupTree() throws IOException {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.postList(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/query-group-tree", TreeBean.class);
        }
        return service.queryGroupTree();
    }
    
    @RequestMapping("query-group-one")
    public Result queryGroupOne(Long id) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String, Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("id",id);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/query-group-one", Result.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
        }
        return ajaxReData("group",service.queryOne(id));
    }
    
    @RequestMapping("query-list")
    public Result queryList(Long parentId,String name, String code, @UIParam("pager") Pager pager) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String, Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("parentId",parentId);
            uriVariables.put("name",name);
            uriVariables.put("code",code);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/query-list", Result.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables,pager);
        }
        List<Attribute> list = service.queryListItem(parentId, name, code, pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("列表名", "label"));
        categories.add(new GridResult.Category("标识", "value"));
        return ajaxReGrid("gdata",categories,list,pager);
    }
    
    @RequestMapping("query-list-item")
    public Result queryListItem(String activeKey, String name, @UIParam("pager") Pager pager) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String, Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("activeKey",activeKey);
            uriVariables.put("name",name);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/query-list-item", Result.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables,pager);
        }
        List<Attribute> list = service.queryListItem(activeKey, name, null, pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("选项名", "label"));
        categories.add(new GridResult.Category("选项标识", "value"));
        return ajaxReGrid("gdata",categories,list,pager);
    }
    
    @RequestMapping("query-list-item-one")
    public Result queryListItemOne(Long id) {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String, Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("id",id);
            return microClient.post(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/query-list-item-one", Result.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
        }
        return ajaxReData("item",service.queryOne(id));
    }
    
    @RequestMapping(value = "query-list-by-active-key")
    public List<LabelValue> queryListByActiveKey(String activeKey) throws IOException {
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String, Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("activeKey",activeKey);
            return microClient.postList(propBean.getMicroAppName(), "/sys/dd/param-api-ctl/query-list-by-active-key", LabelValue.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
        }
        return service.queryLabelValuesByActiveKey(activeKey);
    }
}
